﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace PVP
{


    class CoordinatesConversion
    {
        public SortedList<string, Point2D> Point2DBaseCP3 = new SortedList<string, Point2D>();

        public CoordinatesConversion()
        {
            _GetBaseCP3();
            if (Directory.Exists(PVP.LeftConvertedGrpPath))
            {
                Directory.Delete(PVP.LeftConvertedGrpPath);
            }
            Directory.CreateDirectory(PVP.LeftConvertedGrpPath);
            if (Directory.Exists(PVP.RightConvertedGrpPath))
            {
                Directory.Delete(PVP.RightConvertedGrpPath);
            }
            Directory.CreateDirectory(PVP.RightConvertedGrpPath);

            GetDataAndSave();
        }

        public SortedList<string, Point2D> ConvertSingleFile(string strfile)
        {
            SortedList<string, Point2D> surveyCP3 = new SortedList<string, Point2D>();
            SortedList<string, Point2D> usedBaseCP3 = new SortedList<string, Point2D>();
            SortedList<string, Point2D> surveyGrp = new SortedList<string, Point2D>();
            string[] lines = strfile.Split('\n');
            string[] cells = null;
            for (int i = 0; i < lines.Length; i++)
            {
                cells = lines[i].Split(',');
                if (cells[0].StartsWith("3"))
                {

                    surveyCP3.Add(cells[0], new Point2D(double.Parse(cells[1]), double.Parse(cells[2])));
                }
                if (cells[0].StartsWith("8") || cells[0].StartsWith("9"))
                {
                    surveyGrp.Add(cells[0], new Point2D(double.Parse(cells[1]), double.Parse(cells[2])));
                }
            }
            foreach (KeyValuePair<string, Point2D> kvp in surveyCP3)
            {
                if (Point2DBaseCP3.ContainsKey(kvp.Key))
                {
                    usedBaseCP3.Add(kvp.Key, Point2DBaseCP3[kvp.Key]);
                }
            }
            Rotate r = new Rotate(usedBaseCP3, surveyCP3, surveyGrp);
            return r.ConvertedsurveyGrp;
        }

        public void GetDataAndSave()
        {
            SortedList<string, Point2D> Grp = new SortedList<string, Point2D>();
            string[] LeftGrpFiles = Directory.GetFiles(PVP.LeftToleranceGrpPath);
            string[] RightGrpFiles = Directory.GetFiles(PVP.RightToleranceGrpPath);
            for (int i = 0; i < LeftGrpFiles.Length; i++)
            {
                using (StreamReader sr = new StreamReader(LeftGrpFiles[i]))
                {
                    Grp = ConvertSingleFile(sr.ReadToEnd());
                }
                string fname = LeftGrpFiles[i].Substring(LeftGrpFiles[i].LastIndexOf('\\'));
                using (StreamWriter sw = new StreamWriter(PVP.LeftConvertedGrpPath + "\\" + fname))
                {
                    foreach (KeyValuePair<string, Point2D> kvp in Grp)
                    {
                        sw.WriteLine(kvp.Key + "," + kvp.Value.X + "," + kvp.Value.Y);
                    }
                }
            }

            for (int i = 0; i < RightGrpFiles.Length; i++)
            {
                using (StreamReader sr = new StreamReader(RightGrpFiles[i]))
                {
                    Grp = ConvertSingleFile(sr.ReadToEnd());
                }
                string fname = RightGrpFiles[i].Substring(RightGrpFiles[i].LastIndexOf('\\'));
                using (StreamWriter sw = new StreamWriter(PVP.RightConvertedGrpPath + "\\" + fname))
                {
                    foreach (KeyValuePair<string, Point2D> kvp in Grp)
                    {
                        sw.WriteLine(kvp.Key + "," + kvp.Value.X + "," + kvp.Value.Y);
                    }
                }
            }
        }



        /// <summary>
        /// 得到大地CPIII控制点数据 
        /// </summary>
        private void _GetBaseCP3()
        {
            string CP3path = PVP.appPath + "\\CPIII.csv";
            string sline;
            string[] cells = null;
            double x = 0.0;
            double y = 0.0;
            double h = 0.0;
            if (File.Exists(CP3path))
            {
                using (StreamReader sr = new StreamReader(CP3path))
                {
                    while ((sline = sr.ReadLine()) != null)
                    {
                        cells = sline.Split(',');
                        x = double.Parse(cells[1]);
                        y = double.Parse(cells[2]);
                        h = double.Parse(cells[3]);
                        Point2DBaseCP3.Add(cells[0], new Point2D(x, y));
                    }
                }
            }
            else
            {
                MessageBox.Show("程序中没有CPIII控制文件，请重新导入！");
                return;
            }
        }
    }
}
